package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.TraderAndStorageDao.StorageMapper;
import com.hqyj.pojo.ResultVoH;
import com.hqyj.pojo.Storage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.service.IStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-10-03
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements IStorageService {

    @Autowired
    private StorageMapper storageMapper;

    @Override
    public ResultVoH queryStorage(Storage storage) {
        ResultVoH vo = new ResultVoH();
        Page<Storage> page = new Page<>(storage.getPage(),storage.getRow());
        QueryWrapper<Storage> qw=new QueryWrapper<>();

        qw.like(!storage.getStorageType().equals("") && storage.getStorageType() != null,
                "storage_type", storage.getStorageType());
        Page<Storage> pageList = storageMapper.selectPage(page, qw);
        if (pageList.getRecords().size() == 0) {
            vo.setInfo("没有查询到数据");
            return vo;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("totalPage", pageList.getPages());//总页数
        map.put("total", pageList.getTotal());//总条数
        map.put("list", pageList.getRecords());//数据集合

        vo.setSuccess(true);
        vo.setData(map);
        System.out.println(map.values());
        return vo;
    }

    @Override
    public ResultVoH updateByCondition(Storage storage) {
        ResultVoH vo = new ResultVoH();

            int insert = storageMapper.updateById(storage);
            if(insert>0) {
                vo.setSuccess(true);
                vo.setInfo("修改成功");
            }
        else {
            vo.setSuccess(false);
            vo.setInfo("修改失败");
        }


        return vo;
    }

    @Override
    public ResultVoH addStorage(Storage storage) {
        System.out.println(storage.getStorageType());
        ResultVoH vo = new ResultVoH();
         Storage storage1 = storageMapper.queryAll(storage.getStorageType());
        if(Objects.isNull(storage1)){
            int insert = storageMapper.insert(storage);
            if(insert>0) {
                vo.setSuccess(true);
                vo.setInfo("添加成功");
            }
        }else {
            vo.setSuccess(false);
            vo.setInfo("仓库已经存在");
        }
        return vo;
    }

    @Override
    public ResultVoH deleteStorage(Storage storage) {
        ResultVoH vo = new ResultVoH();
        int i = storageMapper.deleteById(storage.getStorageId());
        if(i>0){
            vo.setSuccess(true);
            vo.setInfo("删除成功");
            return vo;
        }else {
            vo.setSuccess(false);
            vo.setInfo("删除失败");
            return vo;
        }
    }
}
